package com.mls.guardian.executor.slave;

import com.mls.guardian.container.SlaveContainer;
import com.mls.guardian.dataobj.ServerProcessInfo;
import com.mls.guardian.protocol.RemoteBody;
import com.mls.guardian.protocol.RemoteCommand;
import com.mls.guardian.protocol.body.ServerProcessInfoBody;
import com.mls.guardian.remoting.executor.RemoteExecutor;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jl on 15/11/25.
 */
public class SlaveServerProcessInfoExecutor implements RemoteExecutor {
    private static final Logger log = LoggerFactory.getLogger(SlaveServerProcessInfoExecutor.class);
    private SlaveContainer container;

    public SlaveServerProcessInfoExecutor(SlaveContainer container) {
        this.container = container;
    }


    @Override
    public RemoteBody execute(ChannelHandlerContext ctx, RemoteCommand request) throws Exception {
        ServerProcessInfoBody serverProcessInfoBody = (ServerProcessInfoBody) request.getRemoteBody();
        log.info("id[{}] code[{}] exec SlaveServerProcessInfoExecutor  start",request.getRequestId(),request.getCode());
        List<ServerProcessInfo> projectList = new ArrayList<ServerProcessInfo>();

        ServerProcessInfo slave = container.getAppController().getProjectAndStatus(serverProcessInfoBody.getProjectName(),container.getRole());
        log.info("id[{}] code[{}] in SlaveServerProcessInfoExecutor  master getProjectAndStatus finish",request.getRequestId(),request.getCode());
        projectList.add(slave);

        serverProcessInfoBody.setProcessInfoList(projectList);

        return serverProcessInfoBody;
    }
}
